﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Diploma.Models;

namespace DiplomaWeb.Models.Developer
{
    public class LogViewModel
    {
        private List<SelectListItem> types;

        public string SelectedType { get; set; }
        public IEnumerable<SelectListItem> Types { get { return types; } }

        public IEnumerable<LogItemViewModel> Items { get; set; }

        public LogViewModel()
        {
            types = new List<SelectListItem>();
            types.Add(new SelectListItem{ Text = "All", Value = ""});
            types.Add(new SelectListItem { Text = "Error", Value = "Error" });
            types.Add(new SelectListItem { Text = "Debug", Value = "Debug" });
            types.Add(new SelectListItem { Text = "Info", Value = "Info" });
            types.Add(new SelectListItem { Text = "SignIn", Value = "SignIn" });
        }

        public void ToViewModel(IEnumerable<DiplomaError> items)
        {
            Items = items.Select(r => new LogItemViewModel
                                          {
                                              Id = r.ErrorID,
                                              Browser = r.Browser,
                                              Exception = r.Exception,
                                              IP = r.IP,
                                              Level = r.Level,
                                              Logger = r.Logger,
                                              Message = r.ErrorMessage,
                                              Occured = r.Occured.HasValue ? r.Occured.Value : new DateTime(),
                                              Type = r.ErrorType.HasValue ? String.Format("{0}", r.ErrorType.Value) : String.Empty,
                                              UserName = r.UserName
                                          });
        }
    }
}